// indexedDBHelper.js  
//https://www.runoob.com/html/html5-web-api-indexeddb.html
var db;
var dbName = "UnityDB";
var dbStoreName = "UnityStore";
var dbVersion = 1;

function db_open() {
    var request = indexedDB.open(dbName, dbVersion);

    request.onupgradeneeded = function (event) {
        db = event.target.result;
        if (!db.objectStoreNames.contains(dbStoreName)) {
            db.createObjectStore(dbStoreName, {keyPath: "k"});
        }
    };

    request.onsuccess = function (event) {
        db = event.target.result;
        // callback(db);
    };

    request.onerror = function (event) {
        console.error("Database error: " + event.target.errorCode);
    };
}

function db_addData(key, content, callback) {
    console.log("addData", key, content, callback);
    var transaction = db.transaction([dbStoreName], "readwrite");
    var objectStore = transaction.objectStore(dbStoreName);
    const getRequest = objectStore.get(key);
    getRequest.onsuccess = function (event) {
        const data = event.target.result;
        if (data) {
            // 更新数据
            data.v = content;
            // 将更新后的数据存回数据库
            const updateRequest = objectStore.put(data);

            updateRequest.onsuccess = function (event) {
                //  console.log('数据已成功更新');
                db_CallBack(callback, event.target.result, true);
            };

            updateRequest.onerror = function (event) {
                db_CallBack(callback, event.target.errorCode, false);
                console.error("Unable to put data: " + event.target.errorCode);
            };
        } else {
            var request = objectStore.add({k: key, v: content});
            request.onsuccess = function (event) {
                db_CallBack(callback, event.target.result, true);
            };

            request.onerror = function (event) {
                db_CallBack(callback, event.target.errorCode, false);
                console.error("Unable to add data: " + event.target.errorCode);
            };
        }
    }
}

function db_getData(id, callback) {
    var transaction = db.transaction([dbStoreName]);
    var objectStore = transaction.objectStore(dbStoreName);

    var request = objectStore.get(id);

    request.onsuccess = function (event) {
        if (request.result) {
            // console.log("[js] getData result: " ,request.result.k, request.result.v);
            db_CallBack(callback, request.result.v, true);
        } else {
            db_CallBack(callback, null, false);
        }
    };

    request.onerror = function (event) {
        db_CallBack(callback, event.target.errorCode, false);
        console.error("Unable to retrieve data: " + event.target.errorCode);
    };
}

function db_delData(key) {
    var transaction = db.transaction([dbStoreName], "readwrite");
    var objectStore = transaction.objectStore(dbStoreName);
    //  console.log("delData",key);
    var request = objectStore.delete(key);

    request.onsuccess = function (event) {
        if (request.result) {
            console.log("[js] delData result: ", request.result);
        } else {
            console.log("[js] delData result: ", request.result);
        }
    };

    request.onerror = function (event) {
        console.error("Unable to retrieve data: " + event.target.errorCode);
    };
}

function db_CallBack(callback, result, isOk) {
    console.log("callback type:", typeof callback)
    if (db_isNumber(callback)&&callback!=0) { //unity 的回调函数是一个指针，数值, 如果为0，unity 可以传递的是null.
        if (isOk) {
            window.unityInstance.Module.dynCall_vi(callback, window.StringToBuffer(result));
        } else {
            window.unityInstance.Module.dynCall_vi(callback, window.StringToBuffer("null"));
        }
    } else if (db_isFunction(callback) && callback) { //兼容js
        if (isOk) {
            callback(result);
        } else {
            callback(null);
        }
    }
}

function db_isNumber(value) {
    return typeof value === "number";
}

function db_isFunction(value) {
    return typeof value === "function";
}